package com.nullpointer.xchatfinal.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nullpointer.xchatfinal.R;
import com.nullpointer.xchatfinal.object.GroupChat;

public class RoomAdapter extends ArrayAdapter<GroupChat> {

	/**
	 * ViewHolder class for layout.<br />
	 * <br />
	 * Auto-created on 2015-01-19 13:56:15 by Android Layout Finder
	 * (http://www.buzzingandroid.com/tools/android-layout-finder)
	 */
	private static class ViewHolder {
		public final LinearLayout rootView;
		public final TextView tvName;
		public final ImageButton ibOption;
		public final ImageView ivAvatar;
		public final TextView tvNum;
		public final LinearLayout llMember;
		public final LinearLayout llJoin;

		private ViewHolder(LinearLayout rootView, TextView tvName,
				ImageButton ibOption, ImageView ivAvatar, TextView tvNum,
				LinearLayout llMember, LinearLayout llJoin) {
			this.rootView = rootView;
			this.tvName = tvName;
			this.ibOption = ibOption;
			this.ivAvatar = ivAvatar;
			this.tvNum = tvNum;
			this.llMember = llMember;
			this.llJoin = llJoin;
		}

		public static ViewHolder create(LinearLayout rootView) {
			TextView tvName = (TextView) rootView.findViewById(R.id.tv_name);
			ImageButton ibOption = (ImageButton) rootView
					.findViewById(R.id.ib_option);
			ImageView ivAvatar = (ImageView) rootView
					.findViewById(R.id.iv_avatar);
			TextView tvNum = (TextView) rootView.findViewById(R.id.tv_num);
			LinearLayout llMember = (LinearLayout) rootView
					.findViewById(R.id.ll_member);
			LinearLayout llJoin = (LinearLayout) rootView
					.findViewById(R.id.ll_join);
			return new ViewHolder(rootView, tvName, ibOption, ivAvatar, tvNum,
					llMember, llJoin);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder vh;
		if (convertView == null) {
			View view = mInflater.inflate(R.layout.item_lv_group, parent, false);
			vh = ViewHolder.create((LinearLayout) view);
			view.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		GroupChat item = getItem(position);

		if (item.getName() != null) {
			vh.tvName.setText(item.getName());
		} else {
			vh.tvName.setText("XGroup");
		}
		String num = item.getNumMember() + " member";
		vh.tvNum.setText(num);

		vh.ibOption.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AlertDialog.Builder aBuilder = new AlertDialog.Builder(context);
				aBuilder.setItems(new String[] { "Report this group" },
						new Dialog.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								switch (which) {
								case 0:
									// Utils.copyTextToClipboard(context, item
									// .getUserInfo().getStatus());
									break;
								default:
									break;
								}
							}
						});

				AlertDialog alertDialog = aBuilder.create();
				alertDialog.setCanceledOnTouchOutside(true);
				alertDialog.show();
			}
		});

		vh.llMember.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});

		vh.llJoin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});

		return vh.rootView;
	}

	private LayoutInflater mInflater;
	private Context context;

	// Constructors
	public RoomAdapter(Context context, List<GroupChat> objects) {
		super(context, 0, objects);
		this.mInflater = LayoutInflater.from(context);
		this.context = context;
	}

	public RoomAdapter(Context context, GroupChat[] objects) {
		super(context, 0, objects);
		this.mInflater = LayoutInflater.from(context);
		this.context = context;
	}
}
